package com.csdj.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.common.enums.AppHttpCodeEnum;
import com.csdj.common.utils.ResponseData;
import com.csdj.common.utils.ResponseResult;
import com.csdj.pojo.IvIninvoice;
import com.csdj.pojo.IvOutinvoice;
import com.csdj.service.IvIninvoiceService;
import com.csdj.service.IvInvoiceService;
import com.csdj.service.IvOutinvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author: DengYuChang
 * @date: 2023-08-28 10:56
 * @description:
 */
@RestController
@RequestMapping("/ivOutinvoice")
public class IvOutinvoiceController {

    @Autowired
    private IvOutinvoiceService ivOutinvoiceService;

    @Autowired
    private IvIninvoiceService ininvoiceService;

    @Autowired
    private IvInvoiceService invoiceService;


    @PostMapping("isAccordWith")
    public ResponseResult isAccordWith(@RequestBody IvOutinvoice ivOutinvoice){
        int countByStartNoAndEndNo1 = ivOutinvoiceService.getCountByStartNoAndEndNo(Integer.parseInt(ivOutinvoice.getStartNo()),Integer.parseInt(ivOutinvoice.getEndNo()));
        if(countByStartNoAndEndNo1>0){
            return  ResponseResult.errorResult(400,"该区间有发票已出库,出库失败,请重新输入");
        }
        int countBystartNoAndendNo2 = ininvoiceService.countBystartNoAndendNo(Integer.parseInt(ivOutinvoice.getStartNo()), Integer.parseInt(ivOutinvoice.getEndNo()));
        if(countBystartNoAndendNo2!=1) {
            return ResponseResult.errorResult(400, "该区间有发票不在范围之内,出库失败,请重新输入");
        }
        IvIninvoice ininvoice = ininvoiceService.getOneBystartNoAndendNo(Integer.parseInt(ivOutinvoice.getStartNo()), Integer.parseInt(ivOutinvoice.getEndNo()));
        if(Integer.parseInt(ininvoice.getStartNo())>Integer.parseInt(ivOutinvoice.getStartNo()) || Integer.parseInt(ininvoice.getEndNo())<Integer.parseInt(ivOutinvoice.getEndNo()) ){
            return  ResponseResult.errorResult(400,"该区间有发票不在范围之内,出库失败,请重新输入");
        }
        return ResponseResult.okResult(ininvoice);
    }


    @PostMapping("add")
    public ResponseResult add(@RequestBody IvOutinvoice ivOutinvoice){
        boolean save = ivOutinvoiceService.save(ivOutinvoice);
        boolean update = invoiceService.Update(Integer.parseInt(ivOutinvoice.getStartNo()), Integer.parseInt(ivOutinvoice.getEndNo()), ivOutinvoice.getID(), ivOutinvoice.getEmpID());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("listmap")
    public ResponseData listMap(@RequestParam(defaultValue="1" ) int PageNum){
        Page<Map> page=new Page<>(PageNum,2);
        Page<Map> listMap = ivOutinvoiceService.listmap(page);
        return ResponseData.ok().putDataValue("page",listMap);
    }

    @GetMapping("listmapByEmp")
    public ResponseData listMapByEmp(@RequestParam(defaultValue="1" ) int PageNum){
        Page<Map> page=new Page<>(PageNum,2);
        Page<Map> listMap = ivOutinvoiceService.listmapByEmp(page);
        return ResponseData.ok().putDataValue("page",listMap);
    }
}
